import { isReferee } from "@/func";
import { WereWolfNextTool } from "@/werewolf/components/nextTool";
import { WereWolfModule } from "@/werewolf/module";
import { DisplaySlotId, ItemStack } from "@minecraft/server";
import { ScoreBoardDataBase } from "sapi-pro";

export class WereWolfVotePhase extends WereWolfModule.State {
    readonly voteDb = new ScoreBoardDataBase("werewolf_vote", "§a狼人杀投票");

    override onEnter(): void {
        this.addComponent(WereWolfNextTool, {
            onUse: () => this.next(),
            getPlayers: () =>
                this.context.players.filter(
                    (p) => p.player != undefined && isReferee(p.player)
                ),
        });

        this.addComponents(this.context.views.vote);
        this.voteDb.setDisplaySlot(DisplaySlotId.Sidebar);
    }

    giveItem() {
        const item = new ItemStack("werewolf:vote");
        this.context.players.forEach((p) => p.giveItem(item));
        this.getComponent(WereWolfNextTool).giveNextItemToReferee();
    }

    next() {
        this.context.players.forEach((p) => p.clear());
        this.popState();
    }

    override onExit(): void {
        this.voteDb.dispose();
    }
}
